<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue002</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.5.1/dist/vue-router.js "></script>
<script src="https://cdn.jsdelivr.net/npm/vuex@3.6.2/dist/vuex.js"></script>
<style>
    .aaa {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .tupian {
        margin-top: 0vh;
        margin-left: 30vh;
    }

    .nihao {
        color: blue;
        text-decoration: none;
    }

    .router-link-exact-active {
        color: #fff;
        background-color: turquoise;
        text-decoration: none;
    }
</style>

<body>
    <div id="app">
        <Keep-Alive include='home'>
            <router-view></router-view>
        </Keep-Alive>
    </div>
    <template id="navComponentTemp">
        <div class="aaa">
            <router-link class="nihao" to="/">班级首页</router-link><br>
            <router-link class="nihao" to="/about">班级详情</router-link><br>
            <router-link class="nihao" to="/person">个人详情</router-link><br>
        </div>
    </template>
    <template id='homeTemp'>
        <div>
            <div>
                <!-- <nav-component></nav-component> -->
                <nav-component></nav-component>
                <button @click="clickBtn">点击加</button>
                <button @click="click">点击减</button>
                <div>点击了{{ $store.state.clicknum }}次</div>
                <span style="text-align: center;">班级首页</span>
            </div>
        </div>
    </template>
    <template id='aboutTemp'>
        <div>
            <nav-component></nav-component>
            <div><img class="tupian" src="img/命运主宰-多萝西.png" width="600px" height="auto"></div>
            <div><span style="text-align: center;">关于班级</span></div>
            <!-- <nav-component></nav-component> -->
        </div>
    </template>
    <template id='personTemp'>
        <div>
            <!-- <button @click='abboneli'>增加一行</button>
            <button @click='abb'>删除一行</button><br>
            <div v-for="(line, index) in lines" :key="index">{{ line }}</div> -->
            <nav-component></nav-component>
            <button @click="clickBtn">点击加</button>
            <button @click="click">点击减</button>
            <!-- <div>点击了{{clicknum}}次</div><br> -->
            <div>点击了{{ $store.state.clicknum }}次</div>
            <div>
                <span style="text-align: center;">个人主页</span>
            </div>
        </div>
    </template>
</body>

<script>
    var NavComponent = Vue.component('nav-component', {
        template: '#navComponentTemp',
    });
    var home = Vue.component('home', {
        template: '#homeTemp', data: function () {
            return {
                clicknum: 0
            }
        },
        methods: {
            // clickBtn: function () {
            //     this.clicknum++;
            // }
            clickBtn() {
                this.$store.commit('increment');
            },
            click() {
                this.$store.commit('decrease');
            }
        },
        abb: function () {
            this.lines.pop(1)
        },
        created: function () {
            console.log("组件被创建了");
        },
        mounted: function () {
            console.log("组件被加载了");
        },
        updated: function () {
            console.log("组件被修改了");
        },
        destroyed: function () {
            console.log("组件被销毁了");
        }
    });
    var about = Vue.component('about', {
        template: '#aboutTemp'
    })
    var person = Vue.component('person', {
        template: '#personTemp', data: function () {
            return {
                // lines: ['这是第一行文字'],
                clicknum: 0
            }
        }, methods: {
            // clickBtn: function () {
            //     this.clicknum++;
            // },// 在你的组件中
            clickBtn() {
                this.$store.commit('increment');
            },
            click() {
                this.$store.commit('decrease');
            }
            // abboneli: function () {
            //     this.lines.push('这是增加')
            // },
            // abb: function () {
            //     this.lines.pop(1)
            // }
        }
    })
    var router = new VueRouter({
        routes: [
            { path: '/', component: home },
            { path: '/about', component: about },
            { path: '/person', component: person },
        ]
    });
    const store = new Vuex.Store({
        state: {
            clicknum: 0
        },
        mutations: {
            increment(state) {
                state.clicknum++;
            },
            decrease(state) {
                state.clicknum--;
            }
        }
    });
    new Vue({
        el: '#app',
        router: router,
        store: store
        // components: {
        //     'nav-component': NavComponent
        // }
    })
    // 在 Vuex 中共享 clicknum 状态
</script>

</html>